<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 客户建议ajax类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @create by allen.qiang
 */

quanli_require('module/base/contentHandler.class.php');

class ajax_custSuggest{

    public function execute() {

        if (isset($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1:     //客户建议列表信息
                    $this->suggestList();
                    break;

                case 2:     //删除客户建议
                    $this->suggestDelete();
                    break;

                case 3:     //处理客户建议
                    $this->suggestHandle();
                    break;

                case 4:     //提交处理客户建议
                    $this->suggestHandleSubmit();
                    break;

                default:
                    $this->toError('参数错误');
                    break;
            }

        } else {

            $this->toError('参数错误');

        }

    }

    /**
     * 返回错误信息
     * @param string $message 错误提示信息
     * @param int $code 错误代码
     */
    private function toError( $message, $code = 400 ) {

        die ( json_encode( array( 'code' => $code, 'message' => $message ) ) );

    }

    /*
     * 客户建议列表信息
     */
    private function suggestList() {

        global $G_PGLOB;

        //客户建议类
        $contentHandler = new contentHandler($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        //查询
        $result_data = $contentHandler->searchCustSuggest( $arg_array );

        //返回数组
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        //判断是否有编辑权限
        $edit_permission = $G_PGLOB->has_permission(20021);

        if ($result_data){

            foreach ($result_data as $val){

                if($val['suggestStatus'] == 0 )
                    $suggestStatus = '建立';
                if($val['suggestStatus'] == 1 )
                    $suggestStatus = '审核';
                if($val['suggestStatus'] == 2 )
                    $suggestStatus = '作废';

                if($edit_permission){

                    //操作
                    if ($val['suggestStatus'] == 0) {

                        $action = '<a href="javascript:void(0);" onclick="suggest.handle(' . $val['suggestID'] . ')">处理</a>&nbsp;';

                    } else{
                        $action = '';
                    }

                    $result_array['rows'][] = array(
                        'id' => $val['suggestID'],
                        'cell' => array(
                            $action,
                            $val['custName'],
                            $val['mobile'],
                            $val['createTime'],
                            $val['content'],
                            $suggestStatus,
                            $val['reply'],
                            $val['auditor'],
                            $val['auditTime']
                        )
                    );

                }else{

                    $result_array['rows'][] = array(
                        'id' => $val['suggestID'],
                        'cell' => array(
                            $val['custName'],
                            $val['mobile'],
                            $val['createTime'],
                            $val['content'],
                            $val['suggestStatus'],
                            $val['reply'],
                            $val['auditor'],
                            $val['auditTime']
                        )
                    );

                }

            }

        }

        echo json_encode($result_array);

    }

    /**
     * 删除客户建议
     */
    private function suggestDelete(){

        global $G_PGLOB;

        $contentHandler = new contentHandler($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        //查询
        $result_data = $contentHandler->suggestDelete( $arg_array );

        echo json_encode($result_data);

    }

    private function suggestHandle(){

        global $G_PGLOB;

        $contentHandler = new contentHandler($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        //处理客户建议
        $result_data = $contentHandler->suggestHandle( $arg_array );

        echo json_encode($result_data);

    }

    private function suggestHandleSubmit(){

        global $G_PGLOB;

        $contentHandler = new contentHandler($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $arg_array['eai'] = $G_PGLOB->EAI;

        $arg_array['auditor'] = $G_PGLOB->currentUserName;

        //处理客户建议
        $result_data = $contentHandler->suggestHandleSubmit( $arg_array );

        echo json_encode($result_data);

    }

}